package com.guyaogg.gulimall.cart.vo;

import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 *
 * @author 顾遥
 */
public class Cart {
    private List<CartItem> items;
    private Integer countNum;
    private Integer countType;
    private BigDecimal totalAmount;
    private BigDecimal reduce = new BigDecimal(0);
    /**TODO Cart 标识是否修改过，作为优化手段*/
    private boolean flag;

    public Integer getCountNum() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        this.countNum = count;
        return this.countNum;
    }

    public Integer getCountType() {
        int count = 0;
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                count++;
            }
        }
        this.countType = count;
        return this.countType;
    }

    public BigDecimal getTotalAmount() {
        // 1.计算已选中的总数
        BigDecimal amount = new BigDecimal(0);
        if (items != null && items.size() > 0) {
            for (CartItem item : items) {
                if (item.getCheck()) {
                    BigDecimal itemTotalPrice = item.getTotalPrice();
                    amount = amount.add(itemTotalPrice);
                }

            }
        }
        // 2.减去忧患
        amount = amount.subtract(this.reduce);
        this.totalAmount = amount;
        return totalAmount;
    }

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
